
package com.loongtech.bi.action.game;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.game.MallStatistics10LevelManager;
import com.loongtech.bi.manager.game.MallStatisticsManager;
import com.loongtech.bi.manager.game.MallStatisticsManager.MallStatistics;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.NumberUtil;
import jodd.util.StringUtil;
import org.datanucleus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏内分析- 商城统计Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class MallStatisticsAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private Map<String, String> reasonItmes = GameParamConfigManager.getItems(CommonGameParam.MALL_NAME); // 商城名称
	private Map<String, String> reasonItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_NAME); // 商城名称
	private Map<String, String> currencytypeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.MALL_SEARCH_CURRENCYTYPE); // 商城货币类型
	private Map<String, String> typeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.ITEM_TYPE);  // 物品类型反转
	private List<MallStatistics> dataList1;
	private List<MallStatistics> dataList2;
	private String param;
	private Integer rolelevel;
	private Integer reason;
	private Map <String,Integer> rolelevelMap;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	@Autowired
	private MallStatisticsManager mallStatisticsManager;
	@Autowired
	private MallStatistics10LevelManager mallStatistics10LevelManager;
	
	// 查询物品名称
	public String getItemType(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (typeItmesRevise.get(key) == null) ? key : typeItmesRevise.get(key); 
	}
	
	//获得商城货币类型
	public String getMallCurrencytype(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (currencytypeItmesRevise.get(key) == null) ? key : currencytypeItmesRevise.get(key); 
	}
	
	//获得商城名称
	public String getMallName(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (reasonItmesRevise.get(key) == null) ? key : reasonItmesRevise.get(key); 
	}
	
	//货币消耗量
	public Long getSumchangenum(Long sumchangenum) {
		return -sumchangenum; 
	}
	
	@PostConstruct
	public void init() {
		//删掉全部
		reasonItmes.remove("全部");
		currencytypeItmesRevise.remove(-99999);
		setRolelevelMap();
	}
	
	public void search() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		// 开始结束时间不能大于当前时间60天
		if (dateStart.getTime() > dateEnd.getTime()) {
			context.addMessage(null, new FacesMessage("提示", "开始时间不能大于结束时间"));
			return;
		}
		
		//不为null 并且不是数字
		if(!StringUtil.isBlank(param) && !NumberUtil.isNumber(param)) {
			context.addMessage(null, new FacesMessage("提示", "物品ID不是有效数字!"));
			return;
		}

        String shangchengReason = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.SHANGCHENG_REASON).keySet().toArray()[1];
        if (StringUtils.isEmpty(shangchengReason) || GameParamConfigManager.ALL_FLAT.equals(shangchengReason)) {
            context.addMessage(null, new FacesMessage("提示", "没有配置 64 消耗原因是商城 的值 !"));
            return;
        }

        dataList1 = new ArrayList<>();
		sysLogManager.addLog(session.getUserName(), "游戏内分析- 商城统计");
		
		StringBuilder currencytype = new StringBuilder();
		currencytype.append("(");
		
		int sum = 0;
		
		//货币类型传递
		for(Map.Entry<String,String> entry : currencytypeItmesRevise.entrySet()) {
			String type = entry.getKey();
			
			if(sum == 0) {
				currencytype.append(type);
			}else{
				currencytype.append("," + type);
			}
			
			sum ++;
		}
		
		currencytype.append(")");

		dataList1 = mallStatisticsManager.getMallStatistics(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,param,rolelevel,currencytype.toString(), shangchengReason);
	}
	
	//根据最后的日期更新角色等级
	public void updateRolelevel() {
		setRolelevelMap();
	}
	
	//设置等级map
	private void setRolelevelMap() {
		rolelevelMap = new LinkedHashMap<String,Integer>();
		rolelevelMap.put("无筛选等级", -1);
		
		List<Object[]> list = mallStatistics10LevelManager.getMallStatistics10LevelByDateEnd(dateStart,dateEnd);
		
		for(Object o :list) {
			int level = NumberUtil.parseInt(o);
			
			int beginLevel = level * 10 + 1;
			//
			int endLevel = (level + 1) * 10;
			
			String str = beginLevel + "-" + endLevel + " 等级";
			
			rolelevelMap.put(str,level);
		}
		
		rolelevelMap.put("总体", -2);
	}
	
	//等级名称
	public String getLevelName(Integer rolelevel) {
		if(rolelevel == -1) {
			return "总体";
		}
		
		int level = NumberUtil.parseInt(rolelevel);
		
		int beginLevel = level * 10 + 1;
		//
		int endLevel = (level + 1) * 10;
		
		return beginLevel + "-" + endLevel + "等级";
	}
	
	//获得元宝的占比
	public String getYuanBaoProportion(Long sumchangenum,Long allSumchangenum) {
		return PageHelper.double2Percent(sumchangenum.doubleValue() / allSumchangenum.doubleValue());
	}
	
	//获得角色的占比
	public String getRoleProportion(Integer rolenum,Integer allRolenum) {
		return PageHelper.double2Percent(rolenum.doubleValue() / allRolenum.doubleValue());
	}
	
	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<MallStatistics> getDataList1() {
		return dataList1;
	}

	public void setDataList1(List<MallStatistics> dataList1) {
		this.dataList1 = dataList1;
	}


	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}


	public Integer getRolelevel() {
		return rolelevel;
	}


	public void setRolelevel(Integer rolelevel) {
		this.rolelevel = rolelevel;
	}


	public Map <String,Integer> getRolelevelMap() {
		return rolelevelMap;
	}


	public void setRolelevelMap(Map <String,Integer> rolelevelMap) {
		this.rolelevelMap = rolelevelMap;
	}


	public List<MallStatistics> getDataList2() {
		return dataList2;
	}


	public void setDataList2(List<MallStatistics> dataList2) {
		this.dataList2 = dataList2;
	}

	public Map<String, String> getReasonItmes() {
		return reasonItmes;
	}

	public void setReasonItmes(Map<String, String> reasonItmes) {
		this.reasonItmes = reasonItmes;
	}

	public Integer getReason() {
		return reason;
	}

	public void setReason(Integer reason) {
		this.reason = reason;
	}
}
